package com.admin.education.services.Implements;

import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.categorys.CategoryTemplateTransfer;
import com.admin.education.dataTransferObject.categorys.CategorysTransfer;
import com.admin.education.dataTransferObject.categorys.Mapper.CategoryMapper;
import com.admin.education.dataTransferObject.categorys.Request.CreateCategoryRequest;
import com.admin.education.dataTransferObject.categorys.Request.UpdateCategoryRequest;
import com.admin.education.dataTransferObject.role.RoleTransfer;
import com.admin.education.dataTransferObject.role.mapper.RoleMapper;
import com.admin.education.models.Categorys;
import com.admin.education.models.QCategorys;
import com.admin.education.models.Role;
import com.admin.education.repositories.CategorysRepository;
import com.admin.education.services.Interfaces.ICategoryService;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Validated
public class CategoryService implements ICategoryService{
    @Autowired
    private CategoryMapper categoryMapper;
    private QCategorys qCategorys;
    @Autowired
    private CategorysRepository categorysRepository;
    public CategoryService(){
        qCategorys = QCategorys.categorys;
    }
    @Override
    public ServicesExcuteResult<List<CategorysTransfer>> getCategorys() {
        ServicesExcuteResult<List<CategorysTransfer>> servicesExcuteResult = new ServicesExcuteResult<>();

        List<Categorys>  categorys= this.categorysRepository.selectFrom(qCategorys).where(qCategorys.valid.eq(true)).fetch();

        servicesExcuteResult.setData(categoryMapper.mapFrom(categorys));
        servicesExcuteResult.setSuccess(true);

        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<List<CategoryTemplateTransfer>> getTemplates(long id) {
        ServicesExcuteResult<List<CategoryTemplateTransfer>> servicesExcuteResult = new ServicesExcuteResult<>();

        Categorys  category= this.categorysRepository.selectFrom(qCategorys).where(qCategorys.valid.eq(true),qCategorys.id.eq(id)).fetchOne();
        if(null!=category) {
            servicesExcuteResult.setData(categoryMapper.mapTemplateFrom(category.getTemplates()));
            servicesExcuteResult.setSuccess(true);
        }
        else{
            servicesExcuteResult.setErrorMessage("分类不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<PageResponse<CategorysTransfer>> queryCategorys(@Valid PageRequest pageRequest) {
        ServicesExcuteResult<PageResponse<CategorysTransfer>> servicesExcuteResult =new ServicesExcuteResult<>();

        JPAQuery<Categorys> categorysJPAQuery = this.categorysRepository.selectFrom(qCategorys);

        long totalCount = categorysJPAQuery.fetchCount();
        categorysJPAQuery = categorysJPAQuery.orderBy(qCategorys.createTime.desc())
                                            .offset((pageRequest.getPageIndex()-1)*pageRequest.getPageSize()).limit(pageRequest.getPageSize());

        List<Categorys> categorys = categorysJPAQuery.fetch();

        List<CategorysTransfer> categorysTransfers = categoryMapper.mapFrom(categorys);
        PageResponse<CategorysTransfer> categorysTransferPageResponse = new PageResponse<>(pageRequest.getPageSize(),pageRequest.getPageIndex(),(int)totalCount,categorysTransfers);

        servicesExcuteResult.setData(categorysTransferPageResponse);
        servicesExcuteResult.setSuccess(true);

        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult createCategory(@Valid CreateCategoryRequest createCategoryRequest) {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        Categorys categorys =categoryMapper.mapFrom(createCategoryRequest);
        categorys.setCreateTime(new Date());

        this.categorysRepository.persist(categorys);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult updateCategory(long categoryId, @Valid UpdateCategoryRequest updateCategoryRequest) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Categorys> categorysJPAQuery = this.categorysRepository.selectFrom(qCategorys).where(qCategorys.id.eq(categoryId));
        if(0!=categorysJPAQuery.fetchCount()){
            Categorys categorys = categorysJPAQuery.fetchOne();

            categorys =categoryMapper.mapFrom(updateCategoryRequest,categorys);

            this.categorysRepository.merge(categorys);
            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("分类不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult setValid(long category) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult= new ServicesExcuteResult();

        JPAQuery<Categorys> categorysJPAQuery = this.categorysRepository.selectFrom(qCategorys).where(qCategorys.id.eq(category));
        if(0!=categorysJPAQuery.fetchCount()){
            Categorys categorys = categorysJPAQuery.fetchOne();

            categorys.setValid(!categorys.isValid());
            this.categorysRepository.merge(categorys);
            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("分类不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<CategorysTransfer> queryCategoryById(long categoryId) throws ResourceNotFoundException {
        ServicesExcuteResult<CategorysTransfer> servicesExcuteResult= new ServicesExcuteResult();

        JPAQuery<Categorys> categorysJPAQuery = this.categorysRepository.selectFrom(qCategorys).where(qCategorys.id.eq(categoryId));
        if(0!=categorysJPAQuery.fetchCount()){
            Categorys categorys = categorysJPAQuery.fetchOne();

            servicesExcuteResult.setData(categoryMapper.mapFrom(categorys));
            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("分类不存在");
        }
        return servicesExcuteResult;
    }
}
